package id.co.shuba.spamobile.activities;

import id.co.shuba.spamobile.App;
import id.co.shuba.spamobile.R;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.text.InputType;

public class Preference extends PreferenceActivity implements OnPreferenceChangeListener {
	private EditTextPreference prefsServer;
	private EditTextPreference prefsPath;
	private EditTextPreference prefsTimeout;
	private EditTextPreference prefsPort;
	private CheckBoxPreference prefsBypassCert;
	private CheckBoxPreference prefsSecure;
	private CheckBoxPreference prefsDebug;
	
	
	
	@SuppressWarnings("deprecation")
	@Override
    protected void onCreate(Bundle savedInstanceState) {        
        super.onCreate(savedInstanceState);
        
        addPreferencesFromResource(R.xml.preferences);
        
        PreferenceScreen pref = getPreferenceScreen();
        //pref.setOnPreferenceChangeListener(this);        
        
        prefsServer = (EditTextPreference) pref.findPreference("ws_server");
        prefsServer.setOnPreferenceChangeListener(this);
        
        prefsPath = (EditTextPreference) pref.findPreference("ws_path");
        prefsPath.setOnPreferenceChangeListener(this);
        
        prefsTimeout = (EditTextPreference) pref.findPreference("ws_timeout");
        prefsTimeout.getEditText().setInputType(InputType.TYPE_CLASS_NUMBER);
        prefsTimeout.setOnPreferenceChangeListener(this);
        
        prefsPort = (EditTextPreference) pref.findPreference("ws_port");
        prefsPort.getEditText().setInputType(InputType.TYPE_CLASS_NUMBER);        
        prefsPort.setOnPreferenceChangeListener(this);
        
        prefsSecure = (CheckBoxPreference) pref.findPreference("ws_secure");
        prefsSecure.setOnPreferenceChangeListener(this);        
        
        prefsBypassCert = (CheckBoxPreference) pref.findPreference("ws_bypass_certificate");
        prefsDebug = (CheckBoxPreference) pref.findPreference("debug");        
        
        prefsServer.setSummary(prefsServer.getText());
        prefsPath.setSummary(prefsPath.getText());
        prefsTimeout.setSummary(prefsTimeout.getText() + " ms");
        prefsPort.setSummary(prefsPort.getText());        

        setIsSecure(prefsSecure.isChecked());
    }

	@Override
	public boolean onPreferenceChange(android.preference.Preference preference,
			Object newValue) {
		if (preference == prefsServer) {
			prefsServer.setSummary(String.valueOf(newValue));
		} else if (preference == prefsPath) {			
			prefsPath.setSummary(String.valueOf(newValue));
		} else if (preference == prefsTimeout) {
			prefsTimeout.setSummary(String.valueOf(newValue) + " ms");
		} else if (preference == prefsPort) {
			prefsPort.setSummary(String.valueOf(newValue));
		} else if (preference == prefsSecure) {
			setIsSecure((Boolean)newValue);
		} else if (preference == prefsDebug) {			
			App.DEBUG = (Boolean)newValue;
		}
		return true;
	}
	
	private void setIsSecure(boolean isSecure) {
		prefsPort.setEnabled(isSecure);
		prefsBypassCert.setEnabled(isSecure);
	}
}
